package cn.edu.neusoft.wuguangsheng.flowerimpression.adapter;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.Utils.TimeUtils;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.DialogThridUtils;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.OtherUserInfoActivity;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.PhotoInfoActivity;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.WeiboDialogUtils;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SheQuInfoBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SheQuInfoPhotoBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SuccessBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.DeleteSheQuModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.service.Service;

public class SheQuAdapter extends BaseAdapter<SheQuInfoBean> {
    String times;
    String user_id;
    private Dialog mDialog;
    public SheQuAdapter(Context context, List<SheQuInfoBean> items, int layoutResourse,String user_id) {

        super(context, items, layoutResourse);
        this.user_id = user_id;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView= LayoutInflater.from(context).inflate(layoutResourse,parent,false);
        SheQuViewHolder viewHolder = new SheQuViewHolder(itemView);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        SheQuViewHolder sheQuViewHolder=(SheQuViewHolder) holder;
        SheQuInfoBean sheQuInfoBean=items.get(position);
        final String username = sheQuInfoBean.getUser_nickname();
        final String content = sheQuInfoBean.getSq_content();
        final String time = sheQuInfoBean.getSq_time();
        final String sex = sheQuInfoBean.getUser_sex();
       // final String userid = sheQuInfoBean.getUser_id();
        final String address = sheQuInfoBean.getSq_address();
        final String userid = sheQuInfoBean.getUser_id().toString().trim();
        final String sqid = sheQuInfoBean.getSq_id();

      //  final SheQuInfoPhotoBean photo = sheQuInfoBean.getSheQuInfoPhotoBean();
        List<SheQuInfoPhotoBean> shequphoto=sheQuInfoBean.getShequ_photo();
      //  Log.e("Tog",shequphoto.get(0).getSqp_photo().toString());
//       if (shequphoto.size() >0){
//
//       }
       int num = shequphoto.size();
        Log.e("Tog",String.valueOf(num));
        final String[] ph = new String[num];
       for (int i =0 ;i<num;i++){
           Log.e("Tog", shequphoto.get(i).getSqp_photo());
           ph[i] =  shequphoto.get(i).getSqp_photo();
       }
    //    Log.e("Tog",aa.toString());
        //final String[] provinceNames = new String[]{"北京", "上海", "广东", "广西", "天津", "重庆", "湖北", "湖南", "河北", "河南", "山东"};

        sheQuViewHolder.mGridView.setAdapter(new MyAdapter(context,ph,ph));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = simpleDateFormat.parse(time);
            times = TimeUtils.getTimeFormatText(date);
        }catch (Exception e){
            e.printStackTrace();
        }

        if (sheQuInfoBean.getUser_photo().equals("1") || sheQuInfoBean.getUser_photo().equals("1.jpg")){
            if (sex.equals("1")){
                sheQuViewHolder.sqitem_userimg.setImageResource(R.mipmap.nanshi);
            }
            else{
                sheQuViewHolder.sqitem_userimg.setImageResource(R.mipmap.nvshi);
            }
        }
        else{
            String img = Service.BASE_URL+"flower_impression/public/uploads/"+sheQuInfoBean.getUser_photo();
            Picasso.with(context).load(img).into(sheQuViewHolder.sqitem_userimg);
        }
        sheQuViewHolder.sqitem_time.setText(times);
        sheQuViewHolder.sqitem_username.setText(username);
      //  sheQuViewHolder.sqitem_username.setText(shequimg);
        sheQuViewHolder.sqitem_content.setText(content);
        sheQuViewHolder.sqitem_address.setText(address);
        if (userid.equals(user_id)){
            sheQuViewHolder.shanchushequ.setText("删除");
        }
        else{
            sheQuViewHolder.shanchushequ.setText("");
        }

        sheQuViewHolder.shanchushequ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Toast.makeText(context,"666",Toast.LENGTH_LONG).show();
                AlertDialog alertDialog1=new AlertDialog.Builder(context)
                        .setIcon(R.drawable.video_icon)
                        .setTitle("温馨提示")
                        .setMessage("您确认要删除这条社区动态吗？")
                        .setNegativeButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                mDialog = WeiboDialogUtils.createLoadingDialog(context, "删除中...");
                                DeleteSheQuModel deleteSheQuModel = new DeleteSheQuModel();
                                deleteSheQuModel.deleteSheQu(sqid, new TListener<SuccessBean>() {
                                    @Override
                                    public void onResponse(SuccessBean successBean) {
                                        if (successBean.getSuccess().equals("1")){
                                            Toast.makeText(context,"删除成功",Toast.LENGTH_SHORT).show();
                                            DialogThridUtils.closeDialog(mDialog);
                                            items.remove(position);
                                            notifyItemRemoved(position);
                                            notifyItemRangeChanged(0,items.size());                        }
                                        else {
                                            Toast.makeText(context,"删除失败",Toast.LENGTH_SHORT).show();
                                            DialogThridUtils.closeDialog(mDialog);
                                        }
                                    }

                                    @Override
                                    public void onFail(String msg) {
                                        Toast.makeText(context,"网络错误",Toast.LENGTH_SHORT).show();
                                        DialogThridUtils.closeDialog(mDialog);
                                    }
                                });

                            }
                        })
                        .setPositiveButton("取消",null)
                        .create();
                alertDialog1.show();


            }
        });


        sheQuViewHolder.sqitem_userimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, OtherUserInfoActivity.class);
                intent.putExtra("userid",userid);
                context.startActivity(intent);
            }
        });
    }

    private static class MyAdapter extends android.widget.BaseAdapter {
        private LayoutInflater layoutInflater;
        private String[] images;
        private String[] text;
        private Context context;

        public MyAdapter(Context context, String[] images, String[] text){
            this.images = images;
            this.text = text;
            this.context = context;
            layoutInflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            return images.length;
        }

        @Override
        public Object getItem(int position) {
            return images[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View v = layoutInflater.inflate(R.layout.grid_item,null);
            ImageView iv = (ImageView) v.findViewById(R.id.iv_gridView_item);
            String img = Service.BASE_URL+"flower_impression/public/uploads/"+images[position];
            Picasso.with(context).load(img).into(iv);
            iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   // Toast.makeText(context,Service.BASE_URL+"flower_impression/public/uploads/"+images[position],Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(context, PhotoInfoActivity.class);
                    intent.putExtra("imgpath",Service.BASE_URL+"flower_impression/public/uploads/"+images[position]);
                    context.startActivity(intent);
                }
            });


           // iv.setImageResource(images[position]);
            //tv.setText(text[position]);
            return v;
        }
    }
}
